#include "xmpp.h"
#include "server.h"
#include <string.h>

static int id = 0;


void (*elements[27])(void *data, const char *name, const char **atts) = {
  &XmppMuet,   &XmppMuet,   &XmppMuet, // @ A B
  &XmppMuet,   &XmppMuet,   &XmppMuet, // C D E
  &XmppMuet,   &XmppMuet,   &XmppMuet, // F G H
  &XmppMuet,   &XmppMuet,   &XmppMuet, // I J K
  &XmppMuet,   &XmppMuet,   &XmppMuet, // L M N
  &XmppMuet,   &XmppMuet,   &XmppMuet, // O P Q
  &XmppMuet,   &XmppStream, &XmppMuet, // R S T
  &XmppMuet,   &XmppMuet,   &XmppMuet, // U V W
  &XmppMuet,   &XmppMuet,   &XmppMuet  // X Y Z
};


/* cherche un attribut d'une balise */
char * find_attribute(const char *name, const char **atts)
{
  int j;
  char *p;
	
  for (j = 0; atts[j]; j+=2)
    {
      if (!strcasecmp(atts[j], name))
	{
	  p = (char *) malloc(strlen(atts[j+1]));
	  strcpy(p, atts[j+1]);
	  return p;
	}
    }
  return NULL;
}


void XmppMuet(){
  fprintf(stdout, "Muet\n");
  return ;
}


void XMLCALL
processingInstruction(void *userData, const char * target, const char * data)
{

  fprintf(stdout, "\n*************************************** target = %s \n", target);
  fprintf(stdout, "\n data = %s \n", data);

}


/* actions en début de balise */
void XMLCALL
startElement(void *data, const char *name, const char **atts)
{ 
  fprintf(stdout, "balise %s\n", name);
  fflush(stdout);
  
  elements[hash_balise(name)](data, name, atts);
}


/* actions en fin de balise */
void XMLCALL
endElement(void *data, const char *name)
{
  
}


/* actions à la rencontre d'un texte */
void HandlerCharacterData(void *data, const char *text, int len){
}


/* traitement lors de l'ouverture d'un stream */
void XMLCALL
XmppStream(void *data, const char *name, const char **atts){

  struct infoClient * own = (struct infoClient *) data;

  fprintf(stdout, "j'entre a STREAM\n");
  
  own->to = find_attribute("to", atts);
  own->from = find_attribute("from", atts);
  own->id = find_attribute("id", atts);
  own->xml_lang = find_attribute("xml:lang", atts);
  own->version = find_attribute("version", atts);  
  own->xmlns = find_attribute("xmlns", atts);  
  own->xmlns_stream = find_attribute("xmlns:stream", atts);  

  
  fprintf(stdout, "to =  %s\n", own->to);
  fprintf(stdout, "from =  %s\n", own->from);
  fprintf(stdout, "id =  %s\n", own->id);
  fprintf(stdout, "xml:lang =  %s\n", own->xml_lang);
  fprintf(stdout, "version =  %s\n", own->version);
  fprintf(stdout, "xmlns =  %s\n", own->xmlns);
  fprintf(stdout, "xmlns:stream =  %s\n", own->xmlns_stream);


  
  /* repondre au client */


  /* <stream:stream ... > */
  char reponse[1000];

  
  char hostname[100]; gethostname(hostname, 100);

  sprintf(reponse, "<stream:stream version='1.0' id='%d' from='%s' xmlns:stream='%s' xmlns='%s'>", 
	  ++id, hostname, "http://etherx.jabber.org/streams", "jabber:client");

  
  sprintf(&(reponse[strlen(reponse)]),"<stream:features><register xmlns='http://jabber.org/features/iq-register'/><auth xmlns='http://jabber.org/features/iq-auth'/><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms></stream:features>");
    
  
  if(write(own->desc, reponse, strlen(reponse)) == -1){
    perror("write");
    exit(EXIT_SUCCESS);
  }

  
}
